﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace QLKS.Views
{
    /// <summary>
    /// Interaction logic for ucTinNhan.xaml
    /// </summary>
    public partial class ucTinNhan : UserControl
    {
        DataTable tbMessage_To = new DataTable();
        DataTable tbMessage_Send = new DataTable();
        DataTable tbMessage_Delete = new DataTable();
        string message_to_title = string.Empty;
        public ucTinNhan()
        {
            InitializeComponent();
            message_to_title = tblEmail_To.Content.ToString();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            mainContent.Title = SysLib.LanguageLib.GetLanguage().Equals("V") ? "QUẢN LÝ TIN NHẮN" : "MESSAGE MANAGER";
            tbMessage_To = Models.TinNhanModel.GetAllMessageTo(frmMain._UserName);
            tbMessage_Send = Models.TinNhanModel.GetAllMessageSend(frmMain._UserName);
            tbMessage_Delete = Models.TinNhanModel.GetAllMessageDelete(frmMain._UserName);
            tblEmail_To.Content = message_to_title + " (" + tbMessage_To.Select("trangthai = 0").Length.ToString() + ") ";
            lstMenu.SelectedIndex = -1;
            lstMenu.SelectedIndex = 0;

        }

        private void ReLoad()
        {
            tbMessage_To = Models.TinNhanModel.GetAllMessageTo(frmMain._UserName);
            tbMessage_Send = Models.TinNhanModel.GetAllMessageSend(frmMain._UserName);
            tbMessage_Send = Models.TinNhanModel.GetAllMessageSend(frmMain._UserName);
            tbMessage_Delete = Models.TinNhanModel.GetAllMessageDelete(frmMain._UserName);
            tblEmail_To.Content = message_to_title + " (" + tbMessage_To.Select("trangthai = 0").Length.ToString() + ") ";
            lstMenu.SelectedIndex = -1;
            lstMenu.SelectedIndex = 0;
        }
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstMenu.SelectedIndex < 0)
                return;
            switch (lstMenu.SelectedIndex)
            {
                case 0: lstboxMessage.ItemsSource = tbMessage_To.DefaultView;
                    break;
                case 1: lstboxMessage.ItemsSource = tbMessage_Send.DefaultView;
                    break;
                case 2: lstboxMessage.ItemsSource = tbMessage_Delete.DefaultView;
                    break;
            }
        }

        private void xobTinNhan_Loaded(object sender, RoutedEventArgs e)
        {
            //DataTable tb = new DataTable();
            //tb.Columns.Add("caption");
            //tb.Rows.Add("gửi mày 1");
            //tb.Rows.Add("gửi mày 2");
            //tb.Rows.Add("gửi mày 3");
            //tb.Rows.Add("gửi mày 4");
            //lstboxMessage.ItemsSource = tb.DefaultView;

        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            frmGuiTinNhan frm = new frmGuiTinNhan();
            frm.ShowDialog();
            if (frm.UpdateSucces)
                ReLoad();
        }

        private void lstboxMessage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstboxMessage.SelectedIndex < 0)
                return;
            string content = string.Empty;
            int matinnhan = -1;
            int trangthai = 0;
            DataView tbView = (DataView)lstboxMessage.ItemsSource;
            content = tbView[lstboxMessage.SelectedIndex]["noidung"].ToString();
            matinnhan = int.Parse(tbView[lstboxMessage.SelectedIndex]["matinnhan"].ToString());
            trangthai = int.Parse(tbView[lstboxMessage.SelectedIndex]["trangthai"].ToString());
            txtNoiDung.Text = content;
            if (trangthai == 0)
            {
                Models.TinNhanModel.SetTrangThai(matinnhan, 1);
                tbMessage_To.Rows[lstboxMessage.SelectedIndex]["trangthai"] = 1;
                tblEmail_To.Content = message_to_title + " (" + tbMessage_To.Select("trangthai = 0").Length.ToString() + ") ";
            }
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            ReLoad();
        }
    }
}
